package at.studentinformation;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.database.Cursor;


public class Point
{
	public String StudentId;		
	public String SubjectId;	   	
	public String SemesterId;		
	public float PointNumber;
	
	public Point()
	{
		StudentId = "";
		SubjectId = "";
		SemesterId = "";
		PointNumber = 0;
	}

	public static Point ExamineFromJsonObject(JSONObject post)
	{
		Point result = new Point();
		try
        {                                       
            result.StudentId = post.getString("StudentId");
            result.SubjectId = post.getString("SubjectId");
            result.SemesterId = post.getString("SemesterId");
            result.PointNumber = Integer.parseInt(post.getString("PointNumber"));
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
        }
		return result;
	}
	
	public static ArrayList<Point> GetListPointFromJson(String input)
	{
		ArrayList<Point> result = new ArrayList<Point>();
		try
        {   			
            JSONArray entries = new JSONArray(input);
            for(int i =0; i<entries.length(); i++)
            {            	
            	JSONObject post = entries.getJSONObject(i);
	            Point temp = Point.ExamineFromJsonObject(post);
	            result.add(temp);
            }
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
        }
		return result;	
	}

	public static Point GetPointByCusor(Cursor c)
	{
		Point result = new Point();
		result.StudentId = c.getString(c.getColumnIndex("StudentId"));
		result.SubjectId = c.getString(c.getColumnIndex("SubjectId"));
		result.SemesterId = c.getString(c.getColumnIndex("SemesterId"));
		result.PointNumber = Float.parseFloat(c.getString(c.getColumnIndex("PointNumber")));
		
		return result;
	}

	public static ArrayList<Point> GetListPointFormCursor(Cursor c)
	{
		ArrayList<Point> result = new ArrayList<Point>();
		while(c.moveToNext())
		{
			Point temp = Point.GetPointByCusor(c);
			result.add(temp);
		}
		return result;
	}
}